package com.wei.controller.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.wei.blog.common.exception.BlogException;
import com.wei.blog.common.help.UserHelp;
import com.wei.dao.VipDao;
import com.wei.entity.VipEntity;
import com.wei.service.UserService;
import com.wei.service.VipService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/vip")
public class VipController {
    @Autowired
    private UserHelp userHelp;
    @Autowired
    private VipService vipService;

    @GetMapping("/add")
    public VipEntity add() {

        Integer uid = userHelp.get().getUid();

        QueryWrapper<VipEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        VipEntity userEntity1 = vipService.getOne(wrapper);
        if (userEntity1!=null){
            return userEntity1;
        }
        else {
            VipEntity vipEntity = new VipEntity();
            vipEntity.setUid(uid);
            vipEntity.setType(0);
            vipEntity.setLevel(0);
            vipEntity.setCreateTime(new Date());

            boolean save = vipService.save(vipEntity);
            if (save){
                QueryWrapper<VipEntity> wrapper1 = new QueryWrapper<>();
                wrapper.eq("uid",uid);
                VipEntity userEntity2 = vipService.getOne(wrapper1);
                return userEntity2;
            }
            else
                throw new BlogException("数据异常");
        }
    }

    @GetMapping("/find")
    public Map findTime() {
        Integer uid = userHelp.get().getUid();
        QueryWrapper<VipEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        VipEntity vipEntity = vipService.getOne(wrapper);
        Date createTime = vipEntity.getCreateTime();
        Map map = new HashMap();
        map.put("createTime",createTime);
        return map;
    }

    @GetMapping("/delete")
    public String delete(){
        Integer uid = userHelp.get().getUid();

        QueryWrapper<VipEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        VipEntity userEntity1 = vipService.getOne(wrapper);
        if (userEntity1==null){
            return "你还不是会员";
        }
        vipService.remove(wrapper);
        return "已取消";
    }

    @GetMapping("/update")
    public VipEntity update(){
        Integer uid = userHelp.get().getUid();

        QueryWrapper<VipEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        VipEntity userEntity1 = vipService.getOne(wrapper);
        if (userEntity1==null){
            return null;
        }
        
        VipEntity vipEntity = new VipEntity();
        UpdateWrapper<VipEntity> updateWrapper = new UpdateWrapper<>();

        if (userEntity1.getType()==0){
            updateWrapper.eq("uid",uid);
            vipEntity.setType(1);
            boolean i = vipService.update(vipEntity, updateWrapper);
        }
        else {
            updateWrapper.eq("uid",uid);
            vipEntity.setType(0);
            boolean i = vipService.update(vipEntity, updateWrapper);
        }

        VipEntity userEntity = vipService.getOne(wrapper);
        return userEntity;
    }

}
